package com.moon.cloud.log.config;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.core.env.Environment;

import com.moon.cloud.log.aspect.LogAspect;
import com.moon.cloud.log.mapper.RequestLogMapper;

@Configuration
@EnableConfigurationProperties(LogProperties.class)
@EnableAspectJAutoProxy
@MapperScan("com.moon.cloud.log.mapper")
public class LogAutoConfiguration {

	@Bean
	public LogAspect logAspect(LogProperties properties,
			RequestLogMapper logMapper,
			Environment environment) {
		// 从环境变量获取服务名称
		String serverName = environment.getProperty("spring.application.name", "unknown");
		LogAspect aspect = new LogAspect();
		aspect.setProperties(properties);
		aspect.setLogMapper(logMapper);
		aspect.setServerName(serverName);
		return aspect;
	}

}
